package com.settlement.system.common.util;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.IoUtils;
import com.settlement.system.listener.ExcelToMapListener;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class EasyExcelImportUtils {
    /**
     * 动态获取全部列和数据体
     * [stream, parseRowNumber]
     *
     * @return {@link List< Map< String, String>>}
     * excelTypeEnum 文件类型 xls csv xlsx
     * @throws
     * @author 李庆伟
     * @date 2022/5/30 11:29
     */
    public static List<Map<String, String>> parseExcelToData(byte[] stream, Integer parseRowNumber, ExcelTypeEnum excelTypeEnum) {
        ExcelToMapListener readListener = new ExcelToMapListener();
        EasyExcelFactory.read(new ByteArrayInputStream(stream)).excelType(excelTypeEnum).registerReadListener(readListener).headRowNumber(parseRowNumber).sheet(0).doRead();
        List<Map<Integer, String>> headList = readListener.getHeadList();
        if (headList.size() == 0) {
            throw new RuntimeException("Excel表头不能为空");
        }
        List<Map<Integer, String>> dataList = readListener.getDataList();
        //获取头部,取最后一次解析的列头数据
        Map<Integer, String> excelHeadIdxNameMap = headList.get(headList.size() - 1);
        //封装数据体
        List<Map<String, String>> excelDataList = new ArrayList<>();
        for (Map<Integer, String> dataRow : dataList) {
            Map<String, String> rowData = new LinkedHashMap<>();
            // 替换一些符号
            excelHeadIdxNameMap.forEach((key, value) -> rowData.put(value, dataRow.get(key) == null ? null : dataRow.get(key).replace("'", "")));
            excelDataList.add(rowData);
        }
        return excelDataList;
    }

    /**
     * 返回导入的所有数据
     * [file]
     *
     * @return {@link List< Map< String, String>>}
     * excelTypeEnum 文件类型 xls csv xlsx
     * @throws
     * @author 李庆伟
     * @date 2022/5/30 11:16
     */
    public static List<Map<String, String>> makeData(MultipartFile file) {
        ExcelTypeEnum excelTypeEnum = getExcelTypeEnum(file.getOriginalFilename());
        InputStream inputStream = null;//转换成输入流
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] stream = new byte[0];
        try {
            stream = IoUtils.toByteArray(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (stream == null || stream.length == 0) {
            return null;
        }
        List<Map<String, String>> dataList = parseExcelToData(stream, 1, excelTypeEnum);//从动态获取全部列和数据体，默认从第一行开始解析数据
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    public static ExcelTypeEnum getExcelTypeEnum(String filename) {
        filename = filename.toLowerCase();
        if (filename.endsWith(".xlsx")) {
            return ExcelTypeEnum.XLSX;
        } else if (filename.endsWith(".xls")) {
            return ExcelTypeEnum.XLS;
        } else if (filename.endsWith(".csv")) {
            return ExcelTypeEnum.CSV;
        }
        return null;
    }
}
